package Test;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import Interact.*;
import Tool.Tool;

public class Judge extends Question{
	/** 题型 */
	public static final int TYPE = 1;		
	public Judge(){
		setBody(new JudgeBody());
		setContent(new Content());
	}
	
	class AnswerMode implements InteractMode{
		@Override
		public void display(Interface inf) throws IOException {
			JudgeBody body = (JudgeBody)getBody();
			inf.printf("%s:&nbsp<input name=\"%s\" type=\"checkbox\"" +
					" value=\"true\"%s />", 
					Tool.toHTML(body.getCont()), getKey(), 
					Boolean.parseBoolean(getContent().getAnswer()) ? " checked" : "" );
			
			body.getIllustration().display(inf);
		}

		@Override
		public void setValueViaInterface(Interface inf) {
			content.setAnswer(Boolean.toString(inf.getValues(getKey()) == null));
		}
	}
	
	class CorrectMode implements InteractMode{
		private String scoreName(){
			return getKey() + "score";
		}
		
		private String commentName(){
			return getKey() + "comment";
		}
		
		@Override
		public void display(Interface inf) throws IOException {
			JudgeBody body = (JudgeBody)getBody();
			// 打印题干
			inf.println(Tool.toHTML(body.getCont()));
			
			// 打印题干的插图
			body.getIllustration().display(inf);
			
			// 满分
			inf.println("满分：" + body.getGrade());
			
			// 打印答案
			inf.println("学生答案：" + Tool.toHTML(content.getAnswer()));
			
			// 正确答案
			inf.println("正确答案：" + body.getAns());
			
			// 输入得分
			inf.printf("在此输入得分(非法输入认为得分为0)：<input name=\"%s\" value=\"%d\" /><br/>", scoreName(), content.getScore());
			
			// 输入评语
			inf.printf("<textarea name=%s>%s</textarea>",
					commentName(), content.getComment());
		}

		@Override
		public void setValueViaInterface(Interface inf) {
			String value = inf.getValue(scoreName());
			try{content.setScore(Integer.parseInt(value));}
			catch (Exception e){content.setScore(0);}
			
			value = inf.getValue(commentName());
			content.setComment(value == null ? "" : value);
		}
	}
	
	@Override
	public int correct() {
		JudgeBody body = (JudgeBody)getBody();
		if (Boolean.parseBoolean(content.getAnswer()) == body.getAns())
			content.setScore(body.getGrade());
		else content.setScore(0);
		return content.getScore();
	}
	
	@Override
	public int getType() {
		return TYPE;
	}

	@Override
	void initModes() {
		modes = new InteractMode[]{
			new AnswerMode(),
			new CorrectMode()
		};
	}
}









